package com.work.game.adapter;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.work.game.R;
import com.work.game.home.ui.model.Banner;
import com.work.game.home.ui.model.Good;

import java.util.ArrayList;
import java.util.List;

import cn.hutool.core.util.StrUtil;

public class ItemBannerAdapter extends RecyclerView.Adapter<ItemBannerViewHolder> {
    List<Banner> bannerList=new ArrayList<>();
    Context context;

    public ItemBannerAdapter( Context context,List<Banner> bannerList) {
        this.bannerList = bannerList;
        this.context = context;
    }

    @NonNull
    @Override
    public ItemBannerViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int i) {
        View inflate = LayoutInflater.from(context).inflate(R.layout.item_banner_image, parent, false);
        return new ItemBannerViewHolder(inflate);
    }

    @Override
    public void onBindViewHolder(@NonNull ItemBannerViewHolder holder, int position) {
        String imageUrl = bannerList.get(position).getPbUrl();
        Glide.with(context)
                .load(imageUrl)
                .fitCenter()
                .apply(new RequestOptions().transform(new RoundedCorners(32)))
                .error(R.drawable.image_error)
                .into(holder.imageView);
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                com.work.game.home.ui.model.Banner bannerVo = bannerList.get(position);
                String link = bannerVo.getPbLink();
                if (StrUtil.isNotEmpty(link) && !"null".equals(link)) {
                    Intent intent = new Intent(Intent.ACTION_VIEW);
                    intent.setData(Uri.parse(link));
                    // 检查是否有应用可以处理这个Intent
                    if (intent.resolveActivity(context.getPackageManager()) != null) {
                        context.startActivity(intent); // 打开浏览器

                    } else {
                        // 如果没有找到应用程序，则提示用户
                        Toast.makeText(context,"没有找到可以打开链接的应用程序",Toast.LENGTH_SHORT).show();
                    }
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return bannerList.size();
    }
}


class ItemBannerViewHolder extends  RecyclerView.ViewHolder{
    ImageView imageView;
    public ItemBannerViewHolder(@NonNull View itemView) {
        super(itemView);
        imageView=itemView.findViewById(R.id.image_banner);
    }
}

